package com.mad.trafficclient.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.mad.trafficclient.R;
import com.mad.trafficclient.util.JsonUtil;
import com.mad.trafficclient.util.LoadingDialog;
import com.mad.trafficclient.util.NetUtil;


public class Fragment_1 extends Fragment implements View.OnClickListener {
    private static final String TAG = "Fragment_1";
    private TextView balance;
    private EditText jine;
    private Button query;
    private Button chongzhi;
    private Spinner spinner;
    private Integer carIds[] = {1, 2, 3};
    private int carId = 1;
    private ArrayAdapter<Integer> arrayAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater
                .inflate(R.layout.fragment_layout01, container, false);

        //初始化
        initView(view);
        queryBalance();//首次进入查询金额
        return view;
    }

    private void initView(View view) {
        balance = view.findViewById(R.id.qian);
        jine = view.findViewById(R.id.et);
        query = view.findViewById(R.id.query);
        chongzhi = view.findViewById(R.id.recharge);
        spinner = view.findViewById(R.id.sp);

        query.setOnClickListener(this);
        chongzhi.setOnClickListener(this);

        arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, carIds);
        spinner.setAdapter(arrayAdapter);

        //下拉框菜单的选择监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override //当选择的时候会调用这个方法
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                carId = position + 1;
                queryBalance();//切换时查询余额
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.query:
                queryBalance();
                break;
            case R.id.recharge:
                recharge();
                break;
        }
    }

    //充值
    private void recharge() {
        String json = "{\"CarId\":" + carId + ", \"Money\":" + jine.getText().toString() + "}";
        String action = "SetCarAccountRecharge.do";

        new NetUtil(getContext()).async(json, action, new NetUtil.OnRequestResult() {
            @Override
            public void onFailure(Exception e) {
                Log.i(TAG, "onFailure: " + e);
            }

            @Override
            public void onResponse(String result) {
                // result= {'result':'ok'}  <- 解析这串json
                final String json = (String) JsonUtil.getJson(result, "result");

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LoadingDialog.showToast(getContext(),json);
                    }
                });

                //充值成功后查询余额
                queryBalance();
            }
        });
    }

    //充值
    private void queryBalance() {
        String json = "{\"CarId\":" + carId + "}";
        String action = "GetCarAccountBalance.do";

        new NetUtil(getContext()).async(json, action, new NetUtil.OnRequestResult() {
            @Override
            public void onFailure(Exception e) {
                Log.i(TAG, "onFailure: " + e);
            }

            @Override
            public void onResponse(String result) {
                //解析余额出来
                final String yue = (String) JsonUtil.getJson(result, "Balance");

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        balance.setText(yue + "元");//设置查询的余额
                    }
                });
            }
        });
    }
}
